<?php

/**
 * Created by EPP.
 */
class SpecialServerController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('专服管理');
		
		$filter = new PubFilter();
		$grid   = new PubGrid();
		
		$filter->addFilter('name', 'input', '专服名称');
		
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = SpecialServerService::indexHeader();
		if(PubFunc::isAjax()){
			$result = SpecialServerService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=specialServer/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'specialServer/index',
					'length'=>50,
					'page'=>$page
			));
			 
			$this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();

		}
	}

    public function add(){
    	$statusList = SpecialServerService::statusList();
    	$this->out['statusList'] = $statusList;
    	
        $this->setTitle('新建专服');

    }

    public function doAdd(){  	
    	$count = SpecialServerService::repeat(0, 'special_id', $this->R('special_id'),$this->R('g'));
    	if($count > 0){
    		$this->showMessage('专服ID已经存在', PubFunc::buildUrl("/oss/?r=specialServer/index"));
    	}else{
    		$result = SpecialServerService::doAdd($_POST);
    		
    		if($result !== false){
    			$this->showMessage('新增成功', PubFunc::buildUrl("/oss/?r=specialServer/index"));
    		}else{
    			$this->showMessage('新增失败');
    		}
    	}
        
    }

    public function edit(){
        $id = $this->R('id');
        $info = SpecialServerService::get($id);

        if(empty($info)) $this->showMessage('找不到该信息');

        $this->setTitle('编辑专服', $info['name']);

        $this->out['info'] = $info;
        
        $backUrl = PubFunc::buildBackUrl("/oss/?r=specialServer/index");  //返回路径
        $this->out['backUrl'] = $backUrl;
        
        $page = $this->R('page');
        $this->out['page'] = $page;
        
        $statusList = SpecialServerService::statusList();
        $this->out['statusList'] = $statusList;
    }

    public function doEdit(){
        $id = $this->R('id');
        $count = SpecialServerService::repeat($this->R('id'), 'special_id', $this->R('special_id'),$this->R('g'));
        if($count > 0){
        	$this->showMessage('专服ID已经存在');
        }else{
        	$result = SpecialServerService::doEdit($id, $_POST);
        	
        	if($result !== false){
        		$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=specialServer/index"));
        	}else{
        		$this->showMessage('编辑失败');
        	}
        }
        
    }

    /**
     * 删除专服时，判断是否有渠道已经使用了该专服（有则不能删除），同时判断是否服务器使用了该专服（有则不能删除）
     */
    public function delete(){
        $id = $this->R('id');
        
        $modSpecialChannel = new ModSpecialChannel();
        $result_a = $modSpecialChannel->channelForSpecialId($id);
        
        $modSpecialServicer = new ModSpecialServicer();
        $result_b = $modSpecialServicer->serverForSpecialId($id);
        
        if(count($result_a) > 0){
        	$this->showMessage('该专服下存在渠道，不能进行删除',PubFunc::buildBackUrl("/oss/?r=specialServer/index"));
        }else if(count($result_b) > 0){
        	$this->showMessage('该专服下存在服务器，不能进行删除');
        }else{
        	$result = SpecialServerService::delete($id);
        	
        	if($result !== false){
        		$this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=specialServer/index"));
        	}else{
        		$this->showMessage('删除失败', PubFunc::buildBackUrl("/oss/?r=specialServer/index"));
        	}
        }

        
    }
    
    /**
     * 根据专服id获取渠道
     */
    public function getChannel(){
    	$special_id = $this->R('special_id');
    	$result = SpecialServerService::getChannel($special_id);
    	$this->out = $result;
    	$this->outType = 'json';
    }
    
    /**
     * 根据专服id获取服务器、渠道
     */
    public function getServerChannel(){
    	$special_id = $this->R('special_id');
    	$result_1 = SpecialServerService::getChannel($special_id);
    	$result_2 = SpecialServerService::getServer($special_id);
    	if($result_1['status'] == 1 && $result_2['status'] == 1){ 
    		$data['channel'] = $result_1['data'];
    		$data['server'] = $result_2['data'];
    		$list = array();
    		$list['status'] = 1;
    		$list['msg'] = '查询成功';
    		$list['data'] = $data;
    		$this->out = $list;
    	}else if($result_1['status'] == 0){
    		$this->out = $result_1;
    	}else if($result_2['status'] == 0){
    		$this->out = $result_2;
    	}
    	$this->outType = 'json';
    }


}